package com.zhz.mall.portal.controller;

import com.zhz.mall.common.api.CommonResult;
import com.zhz.mall.portal.dto.AddCarDTO;
import com.zhz.mall.portal.dto.CartDeleteDTO;
import com.zhz.mall.portal.dto.CartItemStockDTO;
import com.zhz.mall.portal.dto.CartUpdateQuantityDTO;
import com.zhz.mall.portal.modules.oms.service.OmsCartItemService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhouhengzhe
 * @date 2023/2/4
 */
@RestController
@Api("购物车服务接口")
@RequestMapping("/car")
public class CarController {

    @Autowired
    private OmsCartItemService omsCartItemService;

    /**
     * 添加购物车
     * .post("/cart/add", {
     * productId: this.id,
     * productSkuId: this.skuId,
     * quantity: 1,
     * })
     *
     * @return
     */
    @PostMapping("/add")
    public CommonResult<Boolean> add(@RequestBody AddCarDTO addCarDTO) {
        return omsCartItemService.add(addCarDTO) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 初始化状态栏的购物车商品数量
     * this.axios.get('/car/products/sum').then((res=0)=>{
     */
    @GetMapping("/products/sum")
    public CommonResult<Integer> getCartProductSum() {
        return CommonResult.success(omsCartItemService.getCartProductSum());
    }


    /**
     * 获取购物数据初始化
     * this.axios.get('/car/list')
     */
    @GetMapping("list")
    public CommonResult<List<CartItemStockDTO>> getCartList() {
        return CommonResult.success(omsCartItemService.getCartList());
    }

    /**
     * 更新商品数量
     * this.axios.post('/car/update/quantity',Qs.stringify({
     * id:item.id,
     * quantity:item.quantity   当前数量
     * }),{headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
     */
    @PostMapping("/update/quantity")
    public CommonResult<Boolean> updateQuantity(
            @RequestBody CartUpdateQuantityDTO cartUpdateQuantityDTO) {
        return omsCartItemService.updateQuantity(cartUpdateQuantityDTO) ? CommonResult.success(true) : CommonResult.failed();
    }


    /**
     * 删除
     * this.axios.post('/car/delete',Qs.stringify({
     * ids:item.id
     * }),{headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
     */
    @PostMapping("/delete")
    public CommonResult<Boolean> updateQuantity(
            @RequestBody CartDeleteDTO cartDeleteDTO) {
        return omsCartItemService.delete(cartDeleteDTO.getIds()) ? CommonResult.success(true) : CommonResult.failed();
    }
}
